package Servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import DBOperation.DBOperation;

/**
 * Servlet implementation class PublishFeedServlet
 */
@WebServlet("/PublishFeedServlet")
public class PublishFeedServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PublishFeedServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("--进入servlet--");
		//从session会话中得到userid信息
		HttpSession session = request.getSession(true);
		int userid = (Integer)session.getAttribute("userid");
		//将中文content转为UTF-8编码
		String content = java.net.URLDecoder.decode(request.getParameter("content"),"UTF-8");
		System.out.println("--content--"+content);
		
		DBOperation dbo = new DBOperation();
		int result = dbo.publishFeed(userid, content);
		System.out.println("--result--"+result);
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		out.print(result);
		out.flush();
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request,response);
	}

}
